﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_ListNews : System.Web.UI.Page
{
    // Declare Variables    
    private NewsControl _NewsControl;
    private NewsDTO _NewsDTO;
    public static DataTable _DT_Search;
    private string str_Condition = "";
    private Common obj_Common;

    //Event Page Loaded / Loading
    protected void Page_Load(object sender, EventArgs e)
    {
        _NewsControl = new NewsControl();
        _NewsDTO = new NewsDTO();
        obj_Common = new Common();
        if (!Page.IsPostBack)
        {
            Common.Build_DataTable(_DT_Search, "News", "Yes");
            if (Request.Cookies["Language"].Value.Equals("vi-VN"))
            {
                List_News("Language_ID = 2");
            }
            else
            {
                List_Career("Language_ID = 1");
            }
        }
    }

    //Event Search Data
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            //Check Active
            if (ddl_Search_Active.SelectedIndex > 0)
            {
                str_Condition = "News_Active = " + ddl_Search_Active.SelectedValue;
            }            
            //Check Language
            if (Request.Cookies["Language"].Value.Equals("vi-VN"))
            {
                if (str_Condition != "")
                {
                    str_Condition = str_Condition + " and Language_ID = 2";
                }
                else
                {
                    str_Condition = "Language_ID = 2";
                }
            }
            else
            {
                if (str_Condition != "")
                {
                    str_Condition = str_Condition + " and Language_ID = 1";
                }
                else
                {
                    str_Condition = "Language_ID = 1";
                }
            }
            List_News(str_Condition);
        }
        catch (Exception ex)
        {
            Response.Redirect("error.aspx", false);
        }
        finally
        {
            GC.Collect();
        }
    }

    //Function Row Deleting
    protected void gv_Search_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Delete_News(int.Parse(gv_Search.DataKeys[e.RowIndex].Values[0].ToString()));
    }

    //Event Row Updating
    protected void gv_Search_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string str_NewsID = gv_Search.DataKeys[e.RowIndex].Values[0].ToString();
        Response.Redirect("news_edit.aspx?ID=" + str_NewsID, false);
    }

    //Function Get Data
    public void List_News(string str_Condition)
    {
        if (str_Condition.Equals(""))
        {
            _NewsControl = new NewsControl();
            _DT_Search = _NewsControl.SelectNews();
            if (_DT_Search.Rows.Count > 0)
            {
                gv_Search.Enabled = true;
                gv_Search.DataSource = _DT_Search;
                gv_Search.DataBind();
            }
            else
            {
                Common.Build_DataTable(_DT_Search, "News", "No");
                EmptyRow();
            }
        }
        else
        {
            _NewsControl = new NewsControl();
            _DT_Search = _NewsControl.SelectNews(str_Condition);
            if (_DT_Search.Rows.Count > 0)
            {
                gv_Search.Enabled = true;
                gv_Search.DataSource = _DT_Search;
                gv_Search.DataBind();
            }
            else
            {
                Common.Build_DataTable(_DT_Search, "News", "No");
                EmptyRow();
            }
        }
    }

    //Function Delete Data
    public void Delete_News(int str_NewsID)
    {
        _NewsDTO.News_ID = str_NewsID;
        if (_NewsDTO.DeleteCareer(_VNVN_CareerDTO) == true)
        {
            if (Request.Cookies["Language"].Value.Equals("vi-VN"))
            {
                List_News("Language_ID = 2");
            }
            else
            {
                List_News("Language_ID = 1");
            }
        }
    }

    //Function Add Empty Record
    public void EmptyRow()
    {
        DataRow obj_DR;
        obj_DR = _DT_Search.NewRow();
        obj_DR["News_Active"] = false;
        _DT_Search.Rows.Add(obj_DR);
        gv_Search.DataSource = _DT_Search;
        gv_Search.DataBind();
        gv_Search.Enabled = false;
    }

    // Event PageIndexChanged
    protected void gv_Search_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv_Search.PageIndex = e.NewPageIndex;
        gv_Search.DataSource = _DT_Search;
        gv_Search.DataBind();
    }

    //Event Row Created
    protected void gv_Search_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn_Image = (ImageButton)e.Row.Cells[6].FindControl("img_Delete");
            if (Request.Cookies["Language"].Value.Equals("vi-VN"))
            {
                btn_Image.OnClientClick = "return confirm('Bạn có muốn xó dữ liệu của dòng này không?');";
            }
            else
            {
                btn_Image.OnClientClick = "return confirm('Do you want delete item?');";
            }
        }
    }    
}